import { MessageBoxService, GrowlMessageType, MessageResult } from './../../components/messageBox';
import { ConfigSvr } from './../../services/config';
import { SelectedPrinter, ConfigDeviceComponent } from './configDevice';
import { PrinterManager } from './../../utils/print/printer/printManager';

import { Component, OnInit, Output, EventEmitter, ViewChild } from '@angular/core';

@Component({
    selector: 'printer',
    templateUrl: './printer.component.html',
    styleUrls: ['./printer.component.scss']
})

export class PrinterComponent implements OnInit {
    @Output()
    changePageName: EventEmitter<any> = new EventEmitter<any>();
    @ViewChild(ConfigDeviceComponent) configDevice: ConfigDeviceComponent;
    constructor(
        private printerManager: PrinterManager,
        private configSvr: ConfigSvr
    ) { }
    private printers: any = [];
    private currentName: string = "addDevice";
    private device: boolean;
    private selectedPrinter;
    private isUse: boolean;

    editPrinter(name, printer) {
        this.currentName = name;
        this.selectedPrinter = printer;
        if (this.configDevice) {
            this.configDevice.initPrinter();
        }
    }

    handle(event) {
        if (event == "searchDevice") {
            this.selectedPrinter = null;
        }
        this.currentName = event;
    }

    ngOnInit() {
        this.getPrinterList();
    }

    getPrinterList(event?) {
        if (event) {
            this.printers = event;
        } else {
            this.printers = this.printerManager.getAllDevices();
        }
        if (this.printers && this.printers.length) {
            this.device = true;
            let adsServer = this.configSvr.adsAbout;
            let url = adsServer.url + '/printer/check';
            this.printers.forEach((x) => {
                let checkParams = {
                    'type': x.type,
                    'info': JSON.stringify(x.deviceInfo),
                    'instructionSet': x.instruction.value
                }
                let res = this.printerManager.checkDevice(url, checkParams);
                let printer = this.printerManager.getDeviceById(x.id);
                res.then((data: any) => {
                    if (data.retCode == 0) {
                        if(!x.isConnect){
                            x.isConnect = true;
                        }
                    } else {
                        let errMsg = this.printerManager.errMsg(data);
                        console.log(errMsg);
                        x.isConnect = false;
                    }
                }).catch((error) => {
                    x.isConnect = false;
                    console.log(error);
                });
                this.printerManager.updateDeviceById(printer);
            })
        } else {
            this.device = false;
        }
    }

    async deletePrinter(id) {
        let printer = this.printerManager.getDeviceById(id);
        let info = await MessageBoxService.current.Show("删除确认", "删除 " + printer.deviceName + "？");
        if (info == MessageResult.Ok) {
            let result = this.printerManager.deleteDeviceByID(id);
            if (result.status == 200) {
                this.getPrinterList();
                this.currentName = 'addDevice';
            }
        }
    }

    // 禁用
    disableUse(id) {
        let printerInfo = this.printerManager.getDeviceById(id);
        printerInfo.isUse = !printerInfo.isUse;
        this.printerManager.updateDeviceById(printerInfo);
        this.getPrinterList();
    }

    // 添加外围设备
    addDevice(){
        this.currentName = "deviceType";
    }
    
}
